﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using points.Model;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace points.AppInfo
{
    public static class Util
    {
        public static Customer GetCustomer()
        {
            Customer c = null;

            /*
            Customer c = new Customer
            (
                0,
                null,
                null,
                "n",
                0,
                1, //hard coded 1=end user, 2=merchant
                1,
                this.txtUserid.Text,
                this.txtPassword.Text,
                this.txtSalutation.Text,
                this.txtNameFirst.Text,
                this.txtNameMiddle.Text,
                this.txtNameLast.Text,
                this.txtEmail.Text,
                this.txtWebsite.Text,
                ""
            );
            */
            return c;
        }

        public static Dictionary<string, TypeTable> GetAllFromGenericTypeTable(string tableName)
        {
            //TypeTable tableType = null;
            Dictionary<string, TypeTable> tableTypeHash = new Dictionary<string, TypeTable>();

            string strConn = (string)ConfigurationManager.ConnectionStrings["POINTS_PROD"].ToString();
            DataSet ds = null;

            //SqlParameter paramTableName = new SqlParameter("@paramTableName", SqlDbType.NVarChar);
            //paramTableName.Value = tableName;

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                try
                {
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        ds = new DataSet();
                        string cmdSelect =
                            "SELECT " +
                                "t.pk, " +
                                "t.obj_name, " +
                                "t.add_dt, " +
                                "t.mod_dt, " +
                                "t.marked_delete_y_n, " +
                                "t.rec_id, " +
                                "t.name, " +
                                "t.name_canon, " +
                                "t.descr " +
                            "from " + tableName + " t";
                            //"from @paramTableName t";

                        //cmd.Parameters.Add(paramTableName);

                        using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdSelect, conn))
                        {
                            sqlDataAdapter.Fill(ds);
                        }
                    }
                }
                finally
                {
                    try
                    {
                        conn.Close();
                    }
                    catch { }
                }
            }

            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                if(dt!=null)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        int i = -1;

                        TypeTable tableType = new TypeTable
                        (
                            (Int32)row[++i],
                            (string)row[++i],
                            (DateTime)row[++i],
                            (DateTime)row[++i],
                            (string)row[++i],
                            (Int32)row[++i],
                            (string)row[++i],
                            (string)row[++i],
                            (string)row[++i]
                        );

                        tableTypeHash.Add(tableType.Name_Canon, tableType);
                        /*
                        foreach (DataColumn col in row.)
                        {
                            int x = 1;
                        }
                        */
                    }
                }
            }

            return tableTypeHash;
        }

        /*
        public static TypeTable GetTypeTable(string tableName)
        {
            TypeTable tt = null;


            string strConn = (string)ConfigurationManager.ConnectionStrings["POINTS_PROD"].ToString();


            using (SqlConnection conn = new SqlConnection(strConn))
            {
                DataSet ds = new DataSet();
                string cmdSelect =
                    "SELECT " +
                        "t.pk, " +
                        "t.obj_name, " +
                        "t.add_dt, " +
                        "t.mod_dt, " +
                        "t.marked_delete_y_n, " +
                        "t.rec_id, " +
                        "t.name, " +
                        "t.name_canon, " +
                        "t.descr " +
                    "from " + 
                    tableName + 
                    " t";

                
                using(SqlDataAdapter DataCommand = new SqlDataAdapter(cmdSelect, conn))
                {
                    DataCommand.Fill(ds);
                }
            }

            return tt;
        }
        */
    }
}